package com.langhang.controller;

import com.langhang.entity.TbSpecification;
import com.langhang.entity.TbSpecificationOption;
import com.langhang.service.TbSpecificationOptionService;
import com.langhang.service.TbSpecificationService;
import com.langhang.util.PageResult;
import com.langhang.util.R;
import com.langhang.vo.TbSpecificationvo;
import com.langhang.vo.Tbhb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tbspec")
@CrossOrigin
public class TbSpecificationController {

    @Autowired
    private TbSpecificationService tbSpecificationService;
    @Autowired
    private TbSpecificationOptionService tbSpecificationOptionService;
    //1. 获取学生列表
    @GetMapping("/list")
    public List<TbSpecification> list(){
        return tbSpecificationService.findAll();
    }

    //2. 分页查询学生信息
    @GetMapping("/findByPage")
    public PageResult<TbSpecification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return tbSpecificationService.findByPage(page,pageSize);
    }

    @PostMapping("/search")
    public PageResult<TbSpecification> search(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize, @RequestBody(required = false) TbSpecificationvo tbSpecificationvo){

        return tbSpecificationService.search(page,pageSize,tbSpecificationvo.getSpecName());
    }

    // 4. 保存学生
    @PostMapping("/save")
    public R save(@RequestBody Tbhb tbhb){
        TbSpecification tbspec1=new TbSpecification();
        TbSpecificationOption tbspec2=new TbSpecificationOption();
        tbspec1.setId(tbhb.getId());
        tbspec1.setSpecName(tbhb.getSpecName());
        tbspec2.setId(tbhb.getSpecId());
        tbspec2.setOptionName(tbhb.getOptionName());
        tbspec2.setSpecId(tbhb.getId());
        tbspec2.setOrders(tbhb.getOrders());
        tbSpecificationOptionService.save(tbspec2);

        return tbSpecificationService.save(tbspec1) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }

}
